package com.nbst.comnutil;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
 * 
 * @ClassName: SerializeUtil
 * @Description:序列化工具
 * @author King
 * @date 2018-9-29
 *
 */
public class SerializeUtil {

	/**
	 * 序列化对象成字节码
	 * 
	 * @param object
	 * @return byte[]
	 * @author King
	 * @date 2018-9-29
	 */
	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
			// 序列化
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 把字节码反序列化成对象
	 * 
	 * @param bytes
	 * @return Object
	 * @author King
	 * @date 2018-9-29
	 */
	public static Object unserialize(byte[] bytes) {
		ByteArrayInputStream bais = null;
		if (null == bytes) {
			return null;
		}
		try {
			// 反序列化
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {

		}
		return null;
	}
}